﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TorneoLoL
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
            this.Text = "Login";
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private bool CheckUsuario(String usuario, String pass)
        {
            Conexion conexion = new Conexion();
            DataSet dataS = new DataSet();
            String Consulta = "select * from Usuarios where U_Usuario = '" + usuario + "' and U_Contraseña = '" + pass + "'";
            conexion.consultarDataSet( ref dataS, "Usuarios", Consulta );
            try
            {
                if(dataS.Tables["Usuarios"].Rows[0][0] != null)
                    return true;
                return false;
            }
            catch(System.IndexOutOfRangeException)
            {
                return false;
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
                return false;
            }
        }

        private void btn_aceptar_Click(object sender, EventArgs e)
        {
            if(CheckUsuario(tb_usuario.Text, tb_contraseña.Text))
            {
                FormMenu frm = new FormMenu(0);
                this.Hide();
                frm.Show();
            }
            else
            {
                MessageBox.Show( "Error de autenticación.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
            }
        }

        private void btnVisitante_Click(object sender, EventArgs e)
        {
            FormMenu frm = new FormMenu(1);
            this.Hide();
            frm.Show();
        }

        private void tb_usuario_KeyPress( object sender, KeyPressEventArgs e )
        {
            if(e.KeyChar == 13)
                btn_aceptar_Click(sender, e);
        }

        private void tb_contraseña_KeyPress( object sender, KeyPressEventArgs e )
        {
            if(e.KeyChar == 13)
                btn_aceptar_Click( sender, e );
        }
    }
}
